<?php

namespace app\api\controller;

use app\admin\model\Category;
use app\admin\model\Policy AS PolicyMdl;

class Policy extends BaseController
{

    /**
     * 列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'status' => 1
        ];
        if(isset($param->categoryId) && $param->categoryId){
            $where['categoryId'] = $param->categoryId;
        }
        $fields = 'a.id,a.title,a.imgUrl,a.createDate,c.name';
        $list = PolicyMdl::alias('a')
            ->where($where)
            ->join('category c','a.categoryId = c.id')
            ->field($fields)
            ->order('createDate DESC')
            ->limit($start, $length)
            ->select();
        $total = PolicyMdl::where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到文件通知');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    //获取文件通知分类
    public function getFilesNoticeCategory(){
        $Category = Category::where('c_type',1)->order('sort')->select();
        if($Category){
            return show(config('status.SUCCESS_STATUS'), 'ok', $Category);
        }else{
            return show(config('status.ERROR_STATUS'), '暂无数据', null);
        }
    }


    /**
     * 详情
     */
    public function getDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能为空');
        }

        $where = [
            'a.id' => $param->id
        ];
        $fields = 'a.*,c.name,d.content';
        $item = PolicyMdl::alias('a')
            ->where($where)
            ->join('category c','a.categoryId = c.id')
            ->join('content d','a.id = d.articleId')
            ->field($fields)
            ->find();
        if (empty($item)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到文件通知');
        }

        // 附件
        $fileList = \app\admin\model\Files::where('relatedId', $item->id)->select();
        $item['fileList'] = $fileList;
        foreach($item['fileList'] as $v)
        {
            $file_name = str_replace('http://img.1miaodao.cn/','',$v['file_link']);

            $file_type = substr($v['file_link'],strripos($v['file_link'],'.')+1);
            $v['file_name'] = $file_name;
            $v['file_type'] = $file_type;
        }
        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

}